<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
    <el-breadcrumb-item :to="{ path: '/admin' }" >
      <i class="el-icon-s-promotion"></i> 后台管理
    </el-breadcrumb-item>
    <el-breadcrumb-item>外出申请列表</el-breadcrumb-item>
  </el-breadcrumb>

  <el-divider></el-divider>


  <!-- 事故添加的说明    -->
  <el-card style="float: left ;width: 100%;height: 100%">
    <el-button type="warning" icon="el-icon-edit" @click="">外出申请说明</el-button>
    <div style="width: 100%;text-align: center;margin-top: -30px">
      <img src="../../../assets/outgoing/picture.png" width="1200" alt="">
    </div>
  </el-card>


  <el-card style="float: left ;width: 100%;height: 100%">
    <el-divider content-position="left">外出申请表格</el-divider>
    <el-button type="warning" icon="el-icon-edit" @click="openAddNewDialog()">
      新增外出申请
    </el-button>

    <el-table ref="multipleTable" tooltip-effect="dark" style="width: 100%" border :data="this.outgoingArr">
      <el-table-column type="index" width="100" label="编号" align="center"></el-table-column>
      <el-table-column prop="matter" label="外出事由" align="center"></el-table-column>
      <el-table-column prop="info" width="300" label="外出详情" header-align="center"></el-table-column>
      <el-table-column prop="olderName" label="外出老人" align="center"></el-table-column>
      <el-table-column prop="checkStaffName" label="审核者" align="center"></el-table-column>


      <el-table-column label="审核状态" align="center">
        <template slot-scope="scope">
          <el-tag type="warning" effect="dark" v-if="scope.row.checkState==0">● 正在审核</el-tag>
          <el-tag type="info" effect="dark" v-if="scope.row.checkState==1">● 审核完成</el-tag>
          <el-tag style="background-color: brown" effect="dark" v-if="scope.row.checkState==2">● 驳回!</el-tag>
        </template>
      </el-table-column>

      <el-table-column prop="checkResult" label="审核结果" align="center"></el-table-column>
      <el-table-column prop="gmtCreate" label="提交时间" align="center"></el-table-column>

      <!--操作列-->
      <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark"  content="请注意:外出审核只能审核通过后才可删除!" placement="top-start">
            <el-button v-if="scope.row.checkState==0" type="danger" icon="el-icon-edit"  size="mini"
                       @click="openEditDialog(scope.row)">开始审核</el-button>
            <el-button v-if="scope.row.checkState ==1" type="info" icon="el-icon-delete"  size="mini"
                       @click="openDeleteConfirm(scope.row)">删除</el-button>
            <el-button v-if="scope.row.checkState ==2" type="info" icon="el-icon-delete"  size="mini"
                       @click="openDeleteConfirm(scope.row)">删除</el-button>
          </el-tooltip>
        </template>
      </el-table-column>

    </el-table>

  </el-card>

  <!-- 弹出的新增外出申请的对话框 -->
  <el-dialog title= "新增外出申请"  :visible.sync="addNewFormVisible">
    <el-form :model="addNewForm" :rules="rules" ref="addNewForm">

      <el-form-item label="申请外出老人" :label-width="formLabelWidth"  prop="olderId">
        <el-select v-model="addNewForm.olderId"  >
          <el-option
              v-for="item in olderListOptions" :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="原因概况" :label-width="formLabelWidth" prop="matter">
        <el-input v-model="addNewForm.matter" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="原因详情" :label-width="formLabelWidth" prop="info">
        <el-input v-model="addNewForm.info" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>

    <!-- 新增事件的按钮       -->
    <div slot="footer" class="dialog-footer" style="text-align: center">
      <el-button type="warning" @click="addNewSubmitForm('addNewForm')">提交申请</el-button>
      <el-button @click="addNewFormVisible = false">取 消</el-button>
    </div>
  </el-dialog>

  <!--  弹出的查看修改外出申请的对话框  -->
  <el-dialog style="text-align: center; color: #E6A23C"  title="查看申请" :visible.sync="dialogVisible">

    <el-descriptions class="margin-top" title="" :column="2" :size="size" border>
      <el-descriptions-item label="外出事宜":label-width="formLabelWidth">{{ editForm.matter }}</el-descriptions-item>
      <el-descriptions-item label="外出详细信息":label-width="formLabelWidth">{{ editForm.info }}</el-descriptions-item>
      <el-descriptions-item label="申请老人姓名":label-width="formLabelWidth">{{ editForm.olderName }}</el-descriptions-item>
      <el-descriptions-item label="申请时间":label-width="formLabelWidth">{{ editForm.gmtCreate }}</el-descriptions-item>
    </el-descriptions>

    <el-form :model="editForm" :rules="rules" ref="editForm">
      <el-col :span="24">
        <el-card shadow="never">
          <el-form-item label="审核/驳回意见" :label-width="formLabelWidth" prop="checkResult">
            <el-input type="textarea"  v-model="editForm.checkInfo" autocomplete="off"></el-input>
          </el-form-item>
        </el-card>
      </el-col>
    </el-form>
    <!-- 审核/驳回申请的按钮       -->
    <div slot="footer" class="dialog-footer" style="margin: 0">
      <el-button center= true type="warning" @click="passSubmitForm('editForm')">通过!</el-button>
      <el-button center= true type="info" @click="rejectSubmitForm('editForm')">驳回!</el-button>
      <el-button center= true @click="dialogVisible = false">取 消</el-button>
    </div>
  </el-dialog>



</div>
</template>

<script>
export default {
  data: function () {
    return {
      editForm:{},
      formLabelWidth: '120px',
      addNewFormVisible:false,
      outgoingArr: [],
      olderListOptions:[],
      dialogVisible:false,
      addNewForm:{
        matter:'',
        info:'',
        olderId:'',

      },
      rules: {
        matter: [{ required: true, message: '请简单描述外出原因', trigger: 'blur' }],
        olderId: [{ required: true, message: '选择外出的老人', trigger: 'blur' },],
        info: [{ required: true, message: '请详细描述外出事项及返回时间', trigger: 'blur' }, { min: 3, max: 200, message: '长度在 3 到 200 个字符', trigger: 'blur' }],
        checkResult:[{ required: true, message: '请输入审核/驳回信息', trigger: 'blur' },],
      },
    }
  },
  methods:{
    openDeleteConfirm(outgoing){
      console.log(outgoing);
      let message = '此操作将永久删除【' + outgoing.matter+ '】外出申请，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(outgoing);
      }).catch(() => {
      });
    },
    handleDelete(outgoing) {
      console.log(outgoing.id);
      let url = 'http://localhost:8080/outgoings/' + outgoing.id + "/delete";
      console.log('url = ' + url);

      this.axios.create(
          {
            headers:{
              "Authorization":localStorage.getItem("jwt")
            }
          }
      )
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.loadOutgoingList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadOutgoingList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },




    passSubmitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formData = this.qs.stringify(this.editForm);
          let url = 'http://localhost:8080/outgoings/'+this.editForm.id+'/pass';
          console.log('url = ' + url);
          console.log('formData = ' + formData);
          this.axios
              .create(
                  {
                    headers:{
                      "Authorization":localStorage.getItem("jwt")
                    }
                  }
              )
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '申请通过成功！',
                type: 'success'
              });
              this.resetForm(formName);
              this.loadOutgoingList();
              this.dialogVisible=false;
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });



    },
    rejectSubmitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formData = this.qs.stringify(this.editForm);
          let url = 'http://localhost:8080/outgoings/'+this.editForm.id+'/reject';
          console.log('url = ' + url);
          console.log('formData = ' + formData);
          this.axios
              .create(
                  {
                    headers:{
                      "Authorization":localStorage.getItem("jwt")
                    }
                  }
              )
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '驳回成功！',
                type: 'success'
              });
              this.resetForm(formName);
              this.loadOutgoingList();
              this.dialogVisible=false;
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });




    },

    openEditDialog(formName){
      this.dialogVisible = true;
      let url = 'http://localhost:8080/outgoings/' + formName.id;
      console.log('url = ' + url);
      this.axios.create(
          {
            headers:{
              "Authorization":localStorage.getItem("jwt")
            }
          }
      )
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.editForm = responseBody.data;
          console.log("修改的对象:{}",this.editForm);
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadFoodPackageList();
            }
          });
        }
      });


    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

    addNewSubmitForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8080/outgoings/add-new';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.addNewForm);
          console.log('formData = ' + formData);

          this.axios.create(
              {
                headers:{
                  "Authorization":localStorage.getItem("jwt")
                }
              }
          )
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '事故提交成功！',
                type: 'success'
              });
              this.resetForm(formName);
              this.loadOutgoingList();
              this.addNewFormVisible=false;
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    openAddNewDialog(){
      this.addNewFormVisible=true;
    },

    loadOutgoingList(){
      let url = 'http://localhost:8080/outgoings/list';
      console.log('url = ' + url);
      this.axios.create(
          {
            headers:{
              "Authorization":localStorage.getItem("jwt")
            }
          }
      )
          .get(url).then((response)=>{
        let responseBody=response.data;
        if (responseBody.state==20000){
          this.outgoingArr=responseBody.data;
          console.log('arr={}',this.outgoingArr);
        }
      })
    },
    loadOlderList(){
      let url = 'http://localhost:8080/olders';
      console.log('url = ' + url);
      this.axios.create(
          {
            headers:{
              "Authorization":localStorage.getItem("jwt")
            }
          }
      )

          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.olderListOptions = responseBody.data;
        }
      });

    },
  },
  mounted() {
    this.loadOutgoingList();
    this.loadOlderList();
  },

}
</script>

<style scoped>

</style>